package cn.cdeden.system.controller.system;

import cn.cdeden.common.core.domain.R;
import cn.cdeden.knowledge.api.domain.RemoteCalendarTypeService;
import cn.cdeden.knowledge.api.domain.vo.RemoteCalendarTypeVo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.MapUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Validated
@RequiredArgsConstructor
@RestController
//@Api(tags = "日程类型管理")
@RequestMapping("/api/sysCalendarType")
public class SysCalendarTypeController {

    @DubboReference
    private RemoteCalendarTypeService remoteCalendarTypeService;

    @PostMapping("/sys-calendar-type/create")
//    @Log("新增日程类型")
//    @ApiOperation("新增日程类型")
    public R create (@RequestBody RemoteCalendarTypeVo sysCalendarType) {
        //检查是否有重复的
        List<RemoteCalendarTypeVo> sysCalendarTypeList = remoteCalendarTypeService.listByValue(sysCalendarType.getValue());
        if (!sysCalendarTypeList.isEmpty()) {
            return R.fail("406", "你创建的类型已经存在了");
        }
        List<RemoteCalendarTypeVo> sysCalendarTypes = remoteCalendarTypeService.listByEqName(sysCalendarType.getName());
        if (!sysCalendarTypes.isEmpty()) {
            return R.fail("406", "你创建的类型已经存在了");
        }
        return R.ok(remoteCalendarTypeService.save(sysCalendarType));
    }


    @GetMapping("/get-all-sys-calendar-types")
//    @Log("查询所有日程类型")
//    @ApiOperation("查询所有日程类型")
    public R<List<RemoteCalendarTypeVo>> getAll(@RequestParam Map<String, Object> parameters) {
        String name = MapUtils.getString(parameters, "name");
        return R.ok(remoteCalendarTypeService.getAllByLikeName(name));
    }

    @DeleteMapping("/delete-sys-calendar-type")
//    @Log("删除日程类型")
//    @ApiOperation("删除日程类型")
    public R delete (@RequestParam("id") Long sysCalendarTypeId) {
        return R.ok(remoteCalendarTypeService.removeById(sysCalendarTypeId));
    }

    @PutMapping("update-sys-calendar-type")
//    @Log("修改日程类型")
//    @ApiOperation("修改日程类型")
    public R update(@RequestBody RemoteCalendarTypeVo sysCalendarType) {
        List<RemoteCalendarTypeVo> sysCalendarTypeList = remoteCalendarTypeService.listByUpdate(sysCalendarType);
        if (!sysCalendarTypeList.isEmpty()) {
            return R.fail("406", "你修改提交的类型已经存在了");
        }
        return R.ok(remoteCalendarTypeService.updateById(sysCalendarType));
    }
}
